﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class STK_Grn_Form
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iFormActionOnData As FormActionOnDataEnum
    Dim iDataObject As New InventoryBatchType
    Dim iDataObjectInventory As New InventoryTypeCollection
    Dim iDataObjectStock As New StockItemTypeCollection
    Dim iReCalculationMethodsPushingValue As Boolean = False
#End Region

#Region "Constructors/Destructors"
    Public Overloads Sub Show(DataObject As InventoryBatchType, _
                              Optional DataObjectStock As StockItemTypeCollection = Nothing)
        If DataObject IsNot Nothing Then
            Me.iDataObject = DataObject
            Me.iFormActionOnData = FormActionOnDataEnum.UpdateRecord
            Me.Commit_ImageButton.Enabled = False
        Else
            Me.iDataObject = New InventoryBatchType(Guid.NewGuid, STK_InventoryMethodsEnum.GRN_Entry)
            Me.iFormActionOnData = FormActionOnDataEnum.NewRecord
        End If
        If DataObjectStock IsNot Nothing Then
            Me.iDataObjectStock = DataObjectStock
        Else
            Refresh_StockList_FromDb()
        End If
        MyBase.Show()
        Me.Refresh_ControlsWithDataObject()
    End Sub
    Private Sub STK_Grn_Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Setup_Grid(Me.Inventory_ExtendedDataGridView)

        Setup_CostCalculationMethods()

        Me.InitializeThisFormAndControlSettings()

    End Sub
    Private Sub STK_Grn_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
        Me.StockItem_SearchBox.Dispose()
    End Sub
#End Region

#Region "Events"
    'StockItem_SearchBox
    Private Sub StockItem_SearchBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles StockItem_SearchBox.TextChanged
        Dim sitems As Collections.Generic.IEnumerable(Of StockItemType) = Nothing
        If StockItem_SearchBox.Text <> "" Then
            sitems = From sitem As StockItemType In Me.iDataObjectStock _
                         Where (sitem.ItemCode & _
                                sitem.Description & _
                                sitem.Category & _
                                sitem.SubCategory & _
                                sitem.Brand & _
                                sitem.Model & _
                                sitem.Color & _
                                sitem.Manufacturer & _
                                sitem.CostPrice & _
                                sitem.SellingPrice & _
                                sitem.TaxPercentage).ToLower.Contains(StockItem_SearchBox.Text.ToLower)
                            Select sitem _
                            Order By sitem.ItemCode
        Else
            sitems = From sitem As StockItemType In Me.iDataObjectStock _
                         Select sitem _
                         Order By sitem.ItemCode
        End If
        StockItem_SearchBox.PopupListBox.Items.Clear()
        If (sitems IsNot Nothing) AndAlso (sitems.Count > 0) Then
            For i As Integer = 0 To sitems.Count - 1
                Dim stockItem As StockItemType = sitems(i)
                StockItem_SearchBox.PopupListBox.Items.Add(stockItem.ToDetailsListBoxItem)
            Next
        End If

    End Sub
    Private Sub StockItem_SearchBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles StockItem_SearchBox.SelectedIndexChanged
        If Me.StockItem_SearchBox.SelectedItem IsNot Nothing Then
            Dim det As DetailListBoxItem = Me.StockItem_SearchBox.SelectedItem
            If det.DataObject IsNot Nothing Then
                AddItem_ImageButton.Enabled = True
            Else
                AddItem_ImageButton.Enabled = False
            End If
        Else
            AddItem_ImageButton.Enabled = False
        End If
    End Sub
    'Inventory_ExtendedDataGridView
    Private Sub Inventory_ExtendedDataGridView_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Inventory_ExtendedDataGridView.CellValueChanged
        If (e.ColumnIndex <> -1) AndAlso (e.RowIndex <> -1) Then
            Select Case Me.Inventory_ExtendedDataGridView.Columns(e.ColumnIndex).Name
                Case Is = "QuantityNew_Column"     'new quantity
                    RecalculateGrid_QuantityCell(e.RowIndex)
                Case Is = "CostPriceNew_Column"     'new costprice
                    RecalculateGrid_CostPriceCell(e.RowIndex)
                Case Is = "QuantityAfterInventory_Column"    'new costpriceafterinventory
                    If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(12).Value) = False) Then
                        RecalculateGrid_SellingPriceCell(e.RowIndex)
                    ElseIf (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(13).Value) = False) Then
                        RecalculateGrid_ProfitMarginCell(e.RowIndex)
                    End If
                Case Is = "SellingPriceCurrent_Column"    'current selling price
                    If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(11).Value) = False) Then
                        Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(13).Value = _
                            Me.Inventory_ExtendedDataGridView.Rows(e.RowIndex).Cells(11).Value
                    End If
                Case Is = "ProfitMargin_Column"    'new profitmargin
                    RecalculateGrid_SellingPriceCell(e.RowIndex)
                Case Is = "SellingPriceNew_Column"    'new sellingprice
                    RecalculateGrid_ProfitMarginCell(e.RowIndex)
            End Select
        End If
    End Sub
    Private Sub StockItems_ExtendedDataGridView_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Inventory_ExtendedDataGridView.DataError
        Debug.Print("Data Error in grid........." & e.Exception.ToString)
    End Sub
    Private Sub StockItems_ExtendedDataGridView_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Inventory_ExtendedDataGridView.EditingControlShowing
        If (e.Control IsNot Nothing) AndAlso (TypeOf e.Control Is ComboBox) Then
            Dim cmb As ComboBox = e.Control
            If cmb.Items.Count > 0 Then
                cmb.SelectedIndex = 0
            End If
        End If
    End Sub
    'AddItem_ImageButton
    Private Sub AddItem_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles AddItem_ImageButton.Click
        If Me.StockItem_SearchBox.SelectedItem IsNot Nothing Then
            Dim ditem As DetailListBoxItem = StockItem_SearchBox.SelectedItem
            Dim stockItem As StockItemType = ditem.DataObject
            AddNewInventoryItem(stockItem)
            Me.StockItem_SearchBox.SelectedItem = Nothing
        End If
    End Sub
    'CostCalculationMethod
    Private Sub CostCalculationMethod_StylishComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CostCalculationMethod_StylishComboBox.SelectedIndexChanged
        If CostCalculationMethod_StylishComboBox.SelectedItem IsNot Nothing Then
            For Each row As DataGridViewRow In Me.Inventory_ExtendedDataGridView.Rows
                If row.IsNewRow = False Then
                    RecalculateGrid_CostPriceCell(row.Index)
                End If
            Next
        End If
    End Sub
    'Commit Button
    Private Sub Commit_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Commit_ImageButton.Click

        Refresh_DataObjectWithControls()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Try
            stkMan.Update_Grn(Me.iDataObject, Me.iDataObjectInventory)
            AppSettings.MainForm.StatusInfoMsgPut("GRN Saved")
            Me.OrderRefreshRelatedDataControls(Me.iDataObject, FormActionOnDataEnum.NewRecord)
            Commit_ImageButton.Enabled = False
        Catch ex As DataValidationException
            AMsg.ShowDialog(ex.Message, MsgBoxStyle.Critical, ex, Me.iDataObject)
        Catch ex As Exception
            AMsg.ShowDialog(ex.ToString, MsgBoxStyle.Critical, ex, Me.iDataObject)
        End Try

    End Sub
    'Close Button
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub
#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject

        If (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is InventoryBatchType) _
            AndAlso (CType(DataObject, InventoryBatchType).BatchType = STK_InventoryMethodsEnum.GRN_Entry) Then
            If Me.iDataObject = DataObject Then
                Return Me
            End If
        End If
        Return Nothing

    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.InventoryBatch, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls
        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.StockItem
                Refresh_StockList_FromDb()
        End Select
    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        With Me.iDataObject
            Me.GrnId_TextBox.Text = .InventoryBatchId.ToString
            Me.GRNNumber_TextBox.Text = .GrnReferenceNo
            Me.CommonReference_Number_TextBox.Text = .CommonReferenceNumber
            Me.PONumber_TextBox.Text = .PurchaseOrderNumber
            If .DeliveryDateLoadedDate = DateTime.MinValue Then
                Me.DeliveryDate_DateTimePicker.Value = Now
            Else
                Me.DeliveryDate_DateTimePicker.Value = .DeliveryDateLoadedDate
            End If
            Me.Remarks_TextBox.Text = .Remarks
        End With
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls
        With Me.iDataObject
            .InventoryBatchId = New Guid(Me.GrnId_TextBox.Text)
            .GrnReferenceNo = Me.GRNNumber_TextBox.Text
            .CommonReferenceNumber = Me.CommonReference_Number_TextBox.Text
            .PurchaseOrderNumber = Me.PONumber_TextBox.Text
            .DeliveryDateLoadedDate = Me.DeliveryDate_DateTimePicker.Value
            .Remarks = Me.Remarks_TextBox.Text
        End With
    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)

        If OrderUpdateToFile = False Then AddItem_ImageButton.Enabled = False



    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Setup_CostCalculationMethods()
        CostCalculationMethod_StylishComboBox.Items.Clear()
        Dim items() As STK_InventoryCostCalculationMethods = [Enum].GetValues(GetType(STK_InventoryCostCalculationMethods))
        For Each item As STK_InventoryCostCalculationMethods In items
            CostCalculationMethod_StylishComboBox.Items.Add(item)
        Next
        CostCalculationMethod_StylishComboBox.SelectedItem = STK_InventoryCostCalculationMethods.Replace_With_New_Cost
    End Sub
    Private Sub Refresh_StockList_FromDb()
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Me.iDataObjectStock = stkMan.Get_StockItem_All_Col
    End Sub
    Private Sub Setup_Grid(datagrid As DataGridView)
        '
        'InventoryId_Column
        '
        Dim InventoryId_Column As New DataGridViewTextBoxColumn
        InventoryId_Column.DataPropertyName = "InventoryId"
        InventoryId_Column.HeaderText = "InventoryId"
        InventoryId_Column.Name = "InventoryId_Column"
        InventoryId_Column.Visible = False
        InventoryId_Column.Width = 85
        datagrid.Columns.Add(InventoryId_Column)
        '
        'StockItemId_Column
        '
        Dim StockItemId_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle27 As New DataGridViewCellStyle()
        DataGridViewCellStyle27.BackColor = System.Drawing.Color.Silver
        StockItemId_Column.DefaultCellStyle = DataGridViewCellStyle27
        StockItemId_Column.DataPropertyName = "StockItemId"
        StockItemId_Column.HeaderText = "StockItemId"
        StockItemId_Column.Name = "StockItemId_Column"
        StockItemId_Column.ReadOnly = True
        StockItemId_Column.Visible = False
        StockItemId_Column.Width = 89
        datagrid.Columns.Add(StockItemId_Column)
        '
        'ItemCode_Column
        '
        Dim ItemCode_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle28 As New DataGridViewCellStyle()
        DataGridViewCellStyle28.BackColor = System.Drawing.Color.Silver
        ItemCode_Column.DefaultCellStyle = DataGridViewCellStyle28
        ItemCode_Column.DataPropertyName = "ItemCode"
        ItemCode_Column.HeaderText = "ItemCode"
        ItemCode_Column.Name = "ItemCode_Column"
        ItemCode_Column.ReadOnly = True
        ItemCode_Column.Width = 77
        datagrid.Columns.Add(ItemCode_Column)
        '
        'Description_Column
        '
        Dim Description_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle29 As New DataGridViewCellStyle()
        DataGridViewCellStyle29.BackColor = System.Drawing.Color.Silver
        Description_Column.DefaultCellStyle = DataGridViewCellStyle29
        Description_Column.DataPropertyName = "Description"
        Description_Column.HeaderText = "Description"
        Description_Column.Name = "Description_Column"
        Description_Column.ReadOnly = True
        Description_Column.Width = 85
        datagrid.Columns.Add(Description_Column)
        '
        'QuantityCurrent_Column
        '
        Dim QuantityCurrent_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle30 As New DataGridViewCellStyle()
        DataGridViewCellStyle30.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle30.Format = "N2"
        DataGridViewCellStyle30.NullValue = "0"
        QuantityCurrent_Column.DefaultCellStyle = DataGridViewCellStyle30
        QuantityCurrent_Column.DataPropertyName = "Quantity"
        QuantityCurrent_Column.HeaderText = "Current Quantity"
        QuantityCurrent_Column.Name = "QuantityCurrent_Column"
        QuantityCurrent_Column.ReadOnly = True
        QuantityCurrent_Column.Width = 99
        datagrid.Columns.Add(QuantityCurrent_Column)
        '
        'QuantityNew_Column
        '
        Dim QuantityNew_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle31 As New DataGridViewCellStyle()
        DataGridViewCellStyle31.Format = "N2"
        DataGridViewCellStyle31.NullValue = "0"
        QuantityNew_Column.DefaultCellStyle = DataGridViewCellStyle31
        QuantityNew_Column.DataPropertyName = "InventoryQuantity"
        QuantityNew_Column.HeaderText = "New Quantity"
        QuantityNew_Column.Name = "QuantityNew_Column"
        QuantityNew_Column.Width = 88
        datagrid.Columns.Add(QuantityNew_Column)
        '
        'QuantityAfterInventory_Column
        '
        Dim QuantityAfterInventory_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle32 As New DataGridViewCellStyle()
        DataGridViewCellStyle32.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle32.Format = "N2"
        DataGridViewCellStyle32.NullValue = "0"
        QuantityAfterInventory_Column.DefaultCellStyle = DataGridViewCellStyle32
        QuantityAfterInventory_Column.HeaderText = "Quantity After Inventory"
        QuantityAfterInventory_Column.Name = "QuantityAfterInventory_Column"
        QuantityAfterInventory_Column.ReadOnly = True
        QuantityAfterInventory_Column.Width = 131
        datagrid.Columns.Add(QuantityAfterInventory_Column)
        '
        'CostPriceCurrent_Column
        '
        Dim CostPriceCurrent_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle33 As New DataGridViewCellStyle()
        DataGridViewCellStyle33.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle33.Format = "N2"
        DataGridViewCellStyle33.NullValue = "0"
        CostPriceCurrent_Column.DefaultCellStyle = DataGridViewCellStyle33
        CostPriceCurrent_Column.DataPropertyName = "CostPrice"
        CostPriceCurrent_Column.HeaderText = "Current Cost Price"
        CostPriceCurrent_Column.Name = "CostPriceCurrent_Column"
        CostPriceCurrent_Column.ReadOnly = True
        CostPriceCurrent_Column.Width = 86
        datagrid.Columns.Add(CostPriceCurrent_Column)
        '
        'CostPriceNew_Column
        '
        Dim CostPriceNew_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle34 As New DataGridViewCellStyle()
        DataGridViewCellStyle34.Format = "N2"
        DataGridViewCellStyle34.NullValue = "0"
        CostPriceNew_Column.DefaultCellStyle = DataGridViewCellStyle34
        CostPriceNew_Column.DataPropertyName = "InventoryCostPrice"
        CostPriceNew_Column.HeaderText = "New Cost Price"
        CostPriceNew_Column.Name = "CostPriceNew_Column"
        CostPriceNew_Column.Width = 97
        datagrid.Columns.Add(CostPriceNew_Column)
        '
        'CostPriceAfterInventory_Column
        '
        Dim CostPriceAfterInventory_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle35 As New DataGridViewCellStyle()
        DataGridViewCellStyle35.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle35.Format = "N2"
        DataGridViewCellStyle35.NullValue = "0"
        CostPriceAfterInventory_Column.DefaultCellStyle = DataGridViewCellStyle35
        CostPriceAfterInventory_Column.HeaderText = "Cost Price After Inventory"
        CostPriceAfterInventory_Column.Name = "CostPriceAfterInventory_Column"
        CostPriceAfterInventory_Column.ReadOnly = True
        CostPriceAfterInventory_Column.Width = 139
        datagrid.Columns.Add(CostPriceAfterInventory_Column)
        '
        'SellingPriceCurrent_Column
        '
        Dim SellingPriceCurrentColumn As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle36 As New DataGridViewCellStyle()
        DataGridViewCellStyle36.BackColor = System.Drawing.Color.Silver
        DataGridViewCellStyle36.Format = "N2"
        DataGridViewCellStyle36.NullValue = "0"
        SellingPriceCurrentColumn.DefaultCellStyle = DataGridViewCellStyle36
        SellingPriceCurrentColumn.DataPropertyName = "SellingPrice"
        SellingPriceCurrentColumn.HeaderText = "Current Selling Price"
        SellingPriceCurrentColumn.Name = "SellingPriceCurrent_Column"
        SellingPriceCurrentColumn.ReadOnly = True
        SellingPriceCurrentColumn.Width = 95
        datagrid.Columns.Add(SellingPriceCurrentColumn)
        '
        'ProfitMargin_Column
        '
        Dim ProfitMargin_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle37 As New DataGridViewCellStyle()
        DataGridViewCellStyle37.Format = "N2"
        DataGridViewCellStyle37.NullValue = "0"
        ProfitMargin_Column.DefaultCellStyle = DataGridViewCellStyle37
        ProfitMargin_Column.HeaderText = "Profit Margin"
        ProfitMargin_Column.Name = "ProfitMargin_Column"
        ProfitMargin_Column.Width = 84
        datagrid.Columns.Add(ProfitMargin_Column)
        '
        'SellingPriceNew_Column
        '
        Dim SellingPrice_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle38 As New DataGridViewCellStyle()
        DataGridViewCellStyle38.Format = "N2"
        DataGridViewCellStyle38.NullValue = "0"
        SellingPrice_Column.DefaultCellStyle = DataGridViewCellStyle38
        SellingPrice_Column.DataPropertyName = "InventorySellingPrice"
        SellingPrice_Column.HeaderText = "Selling Price"
        SellingPrice_Column.Name = "SellingPriceNew_Column"
        SellingPrice_Column.Width = 83
        datagrid.Columns.Add(SellingPrice_Column)
        '
        'TaxPercentage_Column
        '
        Dim TaxPercentage_Column As New DataGridViewTextBoxColumn
        Dim DataGridViewCellStyle39 As New DataGridViewCellStyle()
        DataGridViewCellStyle39.Format = "N2"
        DataGridViewCellStyle39.NullValue = "0"
        TaxPercentage_Column.DefaultCellStyle = DataGridViewCellStyle39
        TaxPercentage_Column.DataPropertyName = "TaxPercentage"
        TaxPercentage_Column.HeaderText = "Tax Percentage"
        TaxPercentage_Column.Name = "TaxPercentage_Column"
        TaxPercentage_Column.Width = 99
        datagrid.Columns.Add(TaxPercentage_Column)

    End Sub
    Private Sub AddNewInventoryItem(stockItem As StockItemType)

        Inventory_ExtendedDataGridView.DataSource = Nothing
        Dim inventoryItem As New InventoryType(Guid.NewGuid, stockItem)
        inventoryItem.StockItemId = stockItem.StockItemId
        inventoryItem.RefType = STK_InventoryMethodsEnum.GRN_Entry
        inventoryItem.RefId = Me.iDataObject.InventoryBatchId
        Me.iDataObjectInventory.Add(inventoryItem)
        Inventory_ExtendedDataGridView.AutoGenerateColumns = False
        Inventory_ExtendedDataGridView.DataSource = Me.iDataObjectInventory

    End Sub
    Private Sub RecalculateGrid_QuantityCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityCurrent_Column").Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityNew_Column").Value) = False) Then
                Dim currentQty As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityCurrent_Column").Value
                Dim newQty As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityNew_Column").Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityAfterInventory_Column").Value = currentQty + newQty
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("QuantityAfterInventory_Column").Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_CostPriceCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceCurrent_Column").Value) = False) _
                           AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceNew_Column").Value) = False) Then
                Dim currentCost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceCurrent_Column").Value
                Dim costingMethod As STK_InventoryCostCalculationMethods = CostCalculationMethod_StylishComboBox.SelectedItem
                Dim newCost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceNew_Column").Value
                If costingMethod = STK_InventoryCostCalculationMethods.Moving_Average_Cost Then
                    Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value = (currentCost + newCost) / 2
                Else
                    Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value = newCost
                End If
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_SellingPriceCell(RowIndex As Integer)

        If iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("ProfitMargin_Column").Value) = False) Then
                Dim margin As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("ProfitMargin_Column").Value
                Dim cost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("SellingPriceNew_Column").Value = ((cost * margin) / 100) + cost
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("SellingPriceNew_Column").Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
    Private Sub RecalculateGrid_ProfitMarginCell(RowIndex As Integer)

        If Me.iReCalculationMethodsPushingValue = False Then
            'this is to avoid infinite loop
            Me.iReCalculationMethodsPushingValue = True
            If (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value) = False) _
                            AndAlso (IsDBNull(Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("SellingPriceNew_Column").Value) = False) Then
                Dim sellingPrice As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("SellingPriceNew_Column").Value
                Dim cost As Decimal = Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("CostPriceAfterInventory_Column").Value
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("ProfitMargin_Column").Value = (cost / sellingPrice) * 100
            Else
                Me.Inventory_ExtendedDataGridView.Rows(RowIndex).Cells("ProfitMargin_Column").Value = 0
            End If
            Me.iReCalculationMethodsPushingValue = False
        End If

    End Sub
#End Region








End Class